package com.binc.testspring.study.mina.study1;

import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

import java.util.Objects;

/**
 * FileName: MyHandler 服务端的业务处置对象
 *  必须实现ioHandler  但是实际使用上我们用不到这么多，我们推荐是继承IpHandlerAdapter即可；
 * Autho: binC
 * Date:  2022/3/25 9:40
 */
@Slf4j
// public class MyHandler implements IoHandler {
public class MyServerHandler extends IoHandlerAdapter {

    //这是要重写的   剩下的方法没必要了
    @Override
    public void messageReceived(IoSession session, Object message) throws Exception {
        super.messageReceived(session, message);
        //服务端收到消息}的时候将然后给客户端一个返回
        log.info("-----> server session 收到消息"+message+"【session+】"+session);
        if (Objects.equals("eof", message)) {
            session.close();
        }
        String returnString = "1234567890\r\nqwerty\r\nuiopasdfghjklzxcvbnm,./</TX>";
        String string = "<?xml version=\"1.0\" encoding=\"GB2312\" standalone=\"yes\" ?><TX><REQUEST_SN>942596935052087424</REQUEST_SN><CUST_ID>GZ2000009168314501</CUST_ID><USER_ID>WLPT01</USER_ID><PASSWORD>ado861211</PASSWORD><TX_CODE>6W0100</TX_CODE><LANGUAGE>CN</LANGUAGE><TX_INFO><ACC_NO>52050150383600000006</ACC_NO></TX_INFO></TX>";
        //
        // session.write("车彬</TX>");
        // session.write(string);
        // session.write("开啊</TX>");
        // session.closeNow();
        session.write(returnString);
    }


    /** 下面的方法可以补充些的；为了测试效果才写着玩的 **********************************************************/
    @Override
    public void sessionCreated(IoSession session) throws Exception {
        super.sessionCreated(session);
        log.info("-----> server session创建");
    }

    @Override
    public void sessionOpened(IoSession session) throws Exception {
        super.sessionOpened(session);
        log.info("-----> server session打开");
    }

    @Override
    public void sessionClosed(IoSession session) throws Exception {
        super.sessionClosed(session);
        log.info("-----> server session关闭");
    }

    @Override
    public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
        super.sessionIdle(session, status);
        log.info("-----> server session   idle......");
    }

    @Override
    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
        super.exceptionCaught(session, cause);
        log.info("-----> server session 发生异常");
    }

    @Override
    public void messageSent(IoSession session, Object message) throws Exception {
        super.messageSent(session, message);
        log.info("-----> server session发送消息"+message);
        session.closeNow();
    }
}
